﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.Entities;
using Chinesys.DataModel;

namespace Chinesys.Domain.BizRules
{
    internal static class UserAppInRoleBizRules
    {
        public static void OnSave(UserAppInRoleEntity entity)
        {
            ChinesysEntities db = new ChinesysEntities();

            List<UserAppInRole> list = (from o in db.UserAppInRoles
                                        where o.RoleId == entity.RoleId && o.UserAppId == entity.UserAppId
                                        select o).ToList();

            if (list.Any())
            {
                UserAppInRole errorDetail = list.First();
                throw new BizRulesException(string.Format("El nombre de usuario '{0}' ya existe, para el rol '{1}'. Por favor intente con un rol o un usuario diferente.", errorDetail.UserApp.UserName, errorDetail.Role.RoleName));
            }
        }
    }
}
